package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.table.TableColumnModel;

import com.java.dao.CommentDao;
import com.java.dao.DoctorDao;
import com.java.entity.Comment;
import com.java.entity.Doctor;
import com.java.ext.MyButton;
import com.java.ext.MyJTable;
import com.java.model.DoctorTableModel;

/**
* @ClassName: PatientMainWindow
* @Description: 患者登陆成功之后的主界面
* @author ChenJiaLu
* @date 2023年12月29日 下午2:55:25
*
 */
public class PatientMainWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;

	private List<Doctor> doctors = new ArrayList<Doctor>();

	private Doctor selectObj = null;

	private JTable table = null;

	public PatientMainWindow() {
		setTitle("医院就诊管理系统——患者系统");
		setSize(700, 480);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		initMenu(this);

		DoctorDao dDao = new DoctorDao();
		doctors = dDao.getDoctors();
		for (int i = 0; i < 10; i++) {
			//doctors.addAll(new DoctorDao().getDoctors());
		}
		DoctorTableModel md = new DoctorTableModel(doctors);
		
		table = new MyJTable(md);
		TableColumnModel columnModel = table.getColumnModel();
		columnModel.getColumn(5).setMinWidth(0);  
        columnModel.getColumn(5).setMaxWidth(0);  
        columnModel.getColumn(5).setWidth(0);  
        columnModel.getColumn(5).setPreferredWidth(0); 
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5)); 
		add(scrollPane, BorderLayout.CENTER);

		// 创建输入框和按钮
		JPanel inputPanel = new JPanel();
		inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
		inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		final MyButton orderButton = new MyButton("预约医生").warning();
		final MyButton commentButton = new MyButton("给医生留言").primary();
		inputPanel.add(orderButton);
		inputPanel.add(commentButton);
		add(inputPanel, BorderLayout.SOUTH);
		orderButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("请选中");
					return;
				}
				new TimeslotWindow(bJFrame, selectObj);
			}
		});
		commentButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("请选中");
					return;
				}
				String message = "请输入内容";
	            String title = "留言";
	            String inputValue = JOptionPane.showInputDialog(null, message, title, JOptionPane.QUESTION_MESSAGE); // 显示输入框并获取用户输入的值  
	            if (inputValue != null) {
	            	CommentDao cdao = new CommentDao();
	            	Comment comment = new Comment();
	            	comment.setDoctorid(selectObj.getId());
	            	comment.setPatientid(session.getUserId());
	            	comment.setContent(inputValue);
	            	cdao.saveComment(comment);
	            	ok("留言成功");
	            }
			}
		});

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					selectObj = getObj(row);
					orderButton.setEnabled(true);
					commentButton.setEnabled(true);
				}
			}
		});

		setVisible(true);
	}

	private void setBorder(Border createEmptyBorder) {
		// TODO Auto-generated method stub
		
	}

	public Doctor getObj(int index) {
		for (int i = 0; i < doctors.size(); i++) {
			if (index == i) {
				return doctors.get(i);
			}
		}
		return null;
	}

	public static void main(String[] args) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					new PatientMainWindow();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}